package com.ut.demo.provider;

import com.ut.demo.ResourceTable;
import com.ut.demo.util.ComponentUtil;
import net.steamcrafted.materialiconlib.factory.PixelMapBuilder;
import ohos.agp.components.*;
import ohos.app.Context;

public class GridComponentProvider extends BaseItemProvider {

    private Context context;

    private int[] icons;

    private String[] titles;

    public GridComponentProvider(Context context, int[] icons, String[] titles) {
        this.context = context;
        this.icons = icons;
        this.titles = titles;
    }

    @Override
    public int getCount() {
        return icons == null ? 0 : icons.length/5+icons.length%5;
    }

    @Override
    public Object getItem(int i) {
        return icons[i*5];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ViewHolder holder;
        if (component==null){
            component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_layout, null, false);
            holder = new ViewHolder(component);
            component.setTag(holder);
        }else {
            holder = (ViewHolder) component.getTag();
        }
        bindData(holder, i);
        return component;
    }

    private void bindData(ViewHolder holder, int position){
        holder.image_I.setPixelMap(PixelMapBuilder.with(context).setIconColor(0xFF000000).setResId(icons[position*5]).build());
        holder.text_I.setText(titles[position*5]);
        if (position*5+1>=Math.min(titles.length, icons.length))return;
        holder.image_II.setPixelMap(PixelMapBuilder.with(context).setIconColor(0xFF000000).setResId(icons[position*5+1]).build());
        holder.text_II.setText(titles[position*5+1]);
        if (position*5+2>=Math.min(titles.length, icons.length))return;
        holder.image_III.setPixelMap(PixelMapBuilder.with(context).setIconColor(0xFF000000).setResId(icons[position*5+2]).build());
        holder.text_III.setText(titles[position*5+2]);
        if (position*5+3>=Math.min(titles.length, icons.length))return;
        holder.image_IV.setPixelMap(PixelMapBuilder.with(context).setIconColor(0xFF000000).setResId(icons[position*5+3]).build());
        holder.text_IV.setText(titles[position*5+3]);
        if (position*5+4>=Math.min(titles.length, icons.length))return;
        holder.image_V.setPixelMap(PixelMapBuilder.with(context).setIconColor(0xFF000000).setResId(icons[position*5+4]).build());
        holder.text_V.setText(titles[position*5+4]);
    }

    public static class ViewHolder{

        public Image image_I, image_II, image_III, image_IV, image_V;
        public Text text_I, text_II, text_III, text_IV, text_V ;

        public ViewHolder(Component component) {
            this.image_I = ComponentUtil.findComponent(component, ResourceTable.Id_image_I);
            this.image_II = ComponentUtil.findComponent(component, ResourceTable.Id_image_II);
            this.image_III = ComponentUtil.findComponent(component, ResourceTable.Id_image_III);
            this.image_IV = ComponentUtil.findComponent(component, ResourceTable.Id_image_IV);
            this.image_V = ComponentUtil.findComponent(component, ResourceTable.Id_image_V);
            this.text_I = ComponentUtil.findComponent(component, ResourceTable.Id_text_I);
            this.text_II = ComponentUtil.findComponent(component, ResourceTable.Id_text_II);
            this.text_III = ComponentUtil.findComponent(component, ResourceTable.Id_text_III);
            this.text_IV = ComponentUtil.findComponent(component, ResourceTable.Id_text_IV);
            this.text_V = ComponentUtil.findComponent(component, ResourceTable.Id_text_V);
        }
    }
}
